package com.songlanyun.modules.coupon.model.vo;

import cn.hutool.core.collection.CollectionUtil;
import com.baomidou.mybatisplus.annotation.TableField;
import com.baomidou.mybatisplus.annotation.TableId;
import com.baomidou.mybatisplus.annotation.TableName;
import com.songlanyun.common.drive.RootEntity;
import com.songlanyun.common.enums.CouponConstant;
import com.songlanyun.common.utils.custom.MathUtils;
import com.songlanyun.modules.coupon.entity.CouponReceiveRecordUseEntity;
import com.songlanyun.modules.coupon.service.CouponGoodsService;
import com.songlanyun.modules.coupon.service.CouponReceiveRecordUseService;
import com.songlanyun.modules.coupon.service.CouponShopService;
import com.songlanyun.modules.rabbitmq.util.SpringContextUtil;
import lombok.Data;

import java.io.Serializable;
import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.stream.Collectors;

/**
 * 优惠券领取记录与使用情况
 *
 * @author lubo
 * @email sunlightcs@gmail.com
 * @date 2022-07-05 15:20:02
 */
@Data
@TableName("yxt_coupon_receive_record")
public class CouponReceiveRecordVo extends RootEntity<Long, CouponReceiveRecordVo> implements Serializable {
	private static final long serialVersionUID = 1L;

	/**
	 *
	 */
	@TableId
	private Long id;
	/**
	 * 用户id
	 */
	private Long userId;

	private String mobile;

	private String nickname;

	private String headPhoto;

	private String name;

	private String code;

	private CouponConstant.CouponMode mode;

	/**
	 * 是否是通用券
	 */
	private Boolean universal;

	/**
	 * 优惠券优惠额度
	 */
	private BigDecimal faceValue;
	/**
	 * 是否存在门槛
	 */
	private Boolean hasThreshold;
	/**
	 * 门槛值
	 */
	private BigDecimal threshold;

	/**
	 * 优惠券id
	 */
	private Long couponId;
	/**
	 * 过期时间
	 */
	private Date receiveTime;

	/**
	 * 描述
	 */
	private String content;

	/**
	 * 使用时间
	 */
	private Date useTime;
	/**
	 * 过期时间
	 */
	private Date expireTime;

	/**
	 * 领取后过期天数
	 */
	private Integer expiredDay;
	/**
	 * 0，未使用，1已使用
	 */
	private Boolean useStatus;




	/**
	 * 适合或不适合（如果是平台券非通用券，指的是指定的店铺是适合或不适合，
	 * 如果是店铺券非通用券，指的是指定的商品是适合或不适合）
	 */
	private Boolean adaptOrNo;


	/**
	 * 发行的店铺
	 */
	private Long shopId;

	/**
	 * 开始时间
	 */
	private Date startTime;
	/**
	 * 结束时间
	 */
	private Date endTime;

	/**
	 * 领取数量
	 */
	private Integer receiveNum;

	/**
	 * 每人限领多少张（用了之后给再领取）
	 */
	private Integer receiveLimit;


	/**
	 * 0000001，适应首单优惠1，00000010适应秒杀2，00000100适应拼团4
	 */
	private Integer adaptActivity;

	/**
	 * 发行数量(-1代表不限量)
	 */
	private Integer issueNumber;

	/**
	 * 0:满减券
	 */
	private CouponConstant.CouponType type;

	/**
	 * 是否适配首单优惠
	 * @return
	 */
	public Boolean isAdaptFirstOrder()
	{
		return MathUtils.andOperation(CouponConstant.ADAPT_ACTIVITY.get(CouponConstant.ActivityCouponType.FIRST_ORDER), this.adaptActivity);
	}

	/**
	 * 是否适配秒杀
	 * @return
	 */
	public Boolean isAdaptSecKill()
	{
		return MathUtils.andOperation(CouponConstant.ADAPT_ACTIVITY.get(CouponConstant.ActivityCouponType.SECKILL), this.adaptActivity);
	}

	/**
	 * 是否适配拼团
	 * @return
	 */
	public Boolean isAdaptGroupBuy()
	{
		return MathUtils.andOperation(CouponConstant.ADAPT_ACTIVITY.get(CouponConstant.ActivityCouponType.GROUP_BUY), this.adaptActivity);
	}



	/**
	 * 如果是平台券，那些店铺适合或不适合
	 */
	@TableField(exist = false)
	private List<Long> adaptShops;
	public List<Long> loadAdaptShopIds()
	{
		if(CouponConstant.CouponMode.PLATFORM.equals(this.mode) && adaptShops == null)
		{
			CouponShopService couponShopService = (CouponShopService) SpringContextUtil.getBean("couponShopService");
			adaptShops = couponShopService.loadAdaptShopIds(this.getCouponId());
		}
		return adaptShops;
	}


	/**
	 * 如果是店铺券，那些商品适合或不适合
	 */
	@TableField(exist = false)
	private List<Long> adaptGoods;
	public List<Long> loadAdaptGoodsIds()
	{
		if(CouponConstant.CouponMode.SHOP.equals(this.mode) && adaptGoods == null)
		{
			CouponGoodsService couponGoodsService = (CouponGoodsService) SpringContextUtil.getBean("couponGoodsService");
			adaptGoods = couponGoodsService.loadAdaptGoodIds(this.getShopId(),this.getCouponId());
		}
		return CollectionUtil.isNotEmpty(adaptGoods)?adaptGoods:new ArrayList<>();
	}

	@TableField(exist = false)
	private List<CouponReceiveRecordUseEntity> useRecords;
	public List<CouponReceiveRecordUseEntity> loadUseRecords()
	{
		if(useRecords == null)
		{
			CouponReceiveRecordUseService couponReceiveRecordUseService = (CouponReceiveRecordUseService) SpringContextUtil.getBean("couponReceiveRecordUseService");
			useRecords = couponReceiveRecordUseService.loadByRecordId(this.getId());
		}
		return useRecords;
	}
	@TableField(exist = false)
	private String orderCodes;
	public String loadOrderCodes()
	{
		List<CouponReceiveRecordUseEntity> loadUseRecords = this.loadUseRecords();
		if(!CollectionUtil.isEmpty(loadUseRecords))
		{
			List<String> collect = loadUseRecords.stream().map(CouponReceiveRecordUseEntity::getOrderCode).collect(Collectors.toList());
			orderCodes = String.join(",", collect);
		}
		return orderCodes;
	}


	@Override
	public Long getIdentity() {
		return getId();
	}
}
